require('../../mypublic/css/common.less');
require('../../mypublic/css/index.less');

require('../../public/misc/mediaUpload/css/style.css');
require('../../public/misc/jquery.jqGrid-4.6.0/css/ui.jqgrid.css');
require('../../public/misc/jquery-ui-themes-1.11.2/humanity/jquery-ui.min.css');


require("jquery");

require('../../public/misc/jquery.jqGrid-4.6.0/js/i18n/grid.locale-cn');
require('../../public/misc/jquery.jqGrid-4.6.0/js/jquery.jqGrid.min');

require('../../public/js/myservice/side');
require("../../public/js/myservice/authorize");

oriFunc();

function oriFunc() {
    $(document).ready(function () {
        $('#export-btn').bind('click', function () {
            var ids = $('#grid-table').jqGrid('getGridParam', 'selarrrow');
            window.location = "/api/index.php?r=/official/bespeak/export&ids=" + ids;
        });
        $('#search-btn').bind('click', function () {
            var keyword = $('#search-keyword').val();

            $(this).attr('disabled', 'disabled');
            $('#grid-table').setGridParam(
                {
                    postData: {
                        keyword: keyword
                    }
                }
            );
            $('#grid-table').trigger('reloadGrid');
            $('#search-keyword').focus();
        });

        $('#search-keyword').bind('keypress', function (e) {
            if (e.keyCode == 13) {
                $('#search-btn').trigger('click');
                return false;
            }
            else if (e.keyCode == 27) {
                $('#clean-keyword-btn').trigger('click');
                $(this).val('').blur().focus();
                return false;
            }
        }).focus();

        $('#release-btn').bind('click', function () {
            $(this).attr('disabled', 'disabled');
            setBatch('status', 1);
            $('#grid-table').trigger('reloadGrid');
        });

        $('#cancel-release-btn').bind('click', function () {
            $(this).attr('disabled', 'disabled');
            setBatch('status', 0);
            $('#grid-table').trigger('reloadGrid');
        });

        $('#delete-btn').bind('click', function () {
            $(this).attr('disabled', 'disabled');
            setBatch('del', 1);
            $('#grid-table').trigger('reloadGrid');
        });

        function setBatch(section, val) {
            var list = getList();
            $('#grid-table').setGridParam({
                postData: {
                    ids: list,
                    update: section,
                    value: val
                }
            });
        }

        function getList() {
            var rowData = jQuery('#grid-table').jqGrid('getGridParam', 'selarrrow');
            var list = "";
            if (rowData.length) {
                for (var i = 0; i < rowData.length; i++) {
                    list += ':' + rowData[i];
                }
            }
            return list;
        }

        $('#grid-table').jqGrid(
            {
                url: "/api/index.php?r=/official/bespeak/get-list",
                colNames: [
                    '姓名',
                    '电话',
                    '城市',
                    '公司',
                    '来源',
                    '创建时间',
                ],
                colModel: [
                    {index: 'name', name: 'name', align: 'center', width: 50},
                    {index: 'tel', name: 'tel', align: 'center', width: 50},
                    {index: 'city', name: 'city', align: 'center', width: 50},
                    {index: 'company', name: 'company', align: 'center', width: 50},
                    {index: 'source', name: 'source', align: 'center', width: 20},
                    {index: 'created_at', name: 'created_at', align: 'center', width: 40},
                ],
                datatype: 'json',
                height: 'auto',
                autowidth: true,
                rowNum: 20,
                rowList: [1, 10, 20, 50, 100],
                viewrecords: true,
                viewsortcols: 'vertical',
                hoverrows: true,

                pager: $('#grid-pager'),
                sortname: 'id',
                sortorder: 'asc',
                multiselect: true,
                cellEdit: true,
                cellurl: '/api/index.php?r=/official/bespeak/quick-set',
                afterSubmitCell: function (serverresponse, rowid, cellname, value, iRow, iCol) {
                    if ('' != serverresponse.responseText) {
                        var response = $.parseJSON(serverresponse.responseText);
                        return [response.status, response.content];
                    } else return [true];
                },
                loadComplete: function () {
                    $('#search-btn').attr('disabled', false);
                    $('#release-btn').attr('disabled', false);
                    $('#cancel-release-btn').attr('disabled', false);
                    $('#delete-btn').attr('disabled', false);
                },
                onSelectRow: function (id) {

                }
            }
        );
    });
}
